package controleImpl;

import java.awt.Color;

import interfaceGrafica.GUI;

public class Motion implements Runnable{

	private int nextDestination;
	private int e;
	
	public Motion(int elev){
		nextDestination = -1;
		this.e = elev;
	}

	public void run() {
		try {
			for (;;){
				Thread.sleep(1000);
				if (e == 1){
					verifyDestination(1);
					motion(1);
					
				}
				else{
					verifyDestination(2);
					motion(2);
					
				}
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private void verifyDestination(int elevator){

		if (elevator == 1){
			if (ElevatorController.v().getStatusPlanE1().getDestinations().size() > 0){

				nextDestination = ElevatorController.v().getStatusPlanE1().getDestinations().get(0).getFloor();

			}
			else{
				ElevatorController.v().getMotorE1().stop(1);
				nextDestination = -1;
			}
		}
		else{
			if (ElevatorController.v().getStatusPlanE2().getDestinations().size() > 0){

				nextDestination = ElevatorController.v().getStatusPlanE2().getDestinations().get(0).getFloor();

			}
			else{
				ElevatorController.v().getMotorE2().stop(2);
				nextDestination = -1;
			}
		}
	}
	private void motion(int elevator) throws InterruptedException{
		
		if (elevator == 1){
			int current = ElevatorController.v().getAsE1().getCurrentFloor();
			
			
			if (current == 4){
				ElevatorController.v().getMotorE1().down(1);
			}
			else{
				if (current == 0){
					ElevatorController.v().getMotorE1().up(1);
				}
			}
			boolean up = ElevatorController.v().getMotorE1().isUp();
			
			if (ElevatorController.v().getStatusPlanE1().arrived(1, current, up)){//Chegou no destino
				GUI.v().updateLightDestinationElevator1(current);
				GUI.v().updateLightFloorRequestElevator(current, up);
				GUI.v().LblPortasAbertasE1(Color.BLUE);
				Thread.sleep(5000);
				verifyDestination(1);
			}
			else{
				if (ElevatorController.v().getStatusPlanE1().getDestinations().size() == 1){
					if (ElevatorController.v().getStatusPlanE1().arrived(1, current, !up)){
						ElevatorController.v().getMotorE1().stop(1);
						GUI.v().updateLightDestinationElevator1(current);
						GUI.v().updateLightFloorRequestElevator(current, !up);
						GUI.v().LblPortasAbertasE1(Color.BLUE);
						Thread.sleep(5000);
						verifyDestination(1);
					}
				}
			}
			if (nextDestination != -1){
				if (current == 4){
					ElevatorController.v().getMotorE1().down(1);
				}
				else{
					if (current == 0){
						ElevatorController.v().getMotorE1().up(1);
					}
				}
				if (nextDestination > ElevatorController.v().getAsE1().getCurrentFloor()){

					ElevatorController.v().getMotorE1().up(1);
					ElevatorController.v().getAsE1().setCurrentFloor(1, ElevatorController.v().getAsE1().getCurrentFloor()+1);
					GUI.v().LblPortasAbertasE1(Color.RED);
				}
				else{

					ElevatorController.v().getMotorE1().down(1);
					ElevatorController.v().getAsE1().setCurrentFloor(1, ElevatorController.v().getAsE1().getCurrentFloor()-1);
					GUI.v().LblPortasAbertasE1(Color.RED);
				}
			}
			else{
				ElevatorController.v().getMotorE1().stop(1);
				GUI.v().LblPortasAbertasE1(Color.BLUE);
				Thread.sleep(1000);
			}
		}
		else{
			int current = ElevatorController.v().getAsE2().getCurrentFloor();
			boolean up = ElevatorController.v().getMotorE2().isUp();
			if (ElevatorController.v().getStatusPlanE2().arrived(2, current, up)){
				GUI.v().updateLightDestinationElevator2(current);
				GUI.v().updateLightFloorRequestElevator(current, up);
				GUI.v().LblPortasAbertasE2(Color.BLUE);
				Thread.sleep(1000);
				verifyDestination(2);
			}
			else{
				if (ElevatorController.v().getStatusPlanE2().getDestinations().size() == 1){
					if (ElevatorController.v().getStatusPlanE2().arrived(2, current, !up)){
						ElevatorController.v().getMotorE2().stop(2);
						GUI.v().updateLightDestinationElevator2(current);
						GUI.v().updateLightFloorRequestElevator(current, !up);
						GUI.v().LblPortasAbertasE2(Color.BLUE);
						Thread.sleep(1000);
						verifyDestination(2);
					}
				}
			}
			if (nextDestination != -1){
				if (current == 4){
					ElevatorController.v().getMotorE2().down(2);
				}
				else{
					if (current == 0){
						ElevatorController.v().getMotorE2().up(2);
					}
				}
				if (nextDestination > ElevatorController.v().getAsE2().getCurrentFloor()){

					ElevatorController.v().getMotorE2().up(2);
					ElevatorController.v().getAsE2().setCurrentFloor(2, ElevatorController.v().getAsE2().getCurrentFloor()+1);
					GUI.v().LblPortasAbertasE2(Color.RED);
				}
				else{

					ElevatorController.v().getMotorE2().down(2);
					ElevatorController.v().getAsE2().setCurrentFloor(2, ElevatorController.v().getAsE2().getCurrentFloor()-1);
					GUI.v().LblPortasAbertasE2(Color.RED);
				}
			}
			else{
				ElevatorController.v().getMotorE2().stop(2);
				GUI.v().LblPortasAbertasE2(Color.BLUE);
				Thread.sleep(1000);
			}

		}
	}
}
